package com.yc.Testjvm.invoke;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodHandles.Lookup;
import java.lang.invoke.MethodType;

public class MethodHandleTest2 {
	

	@Override
	public String toString() {
		return super.toString() + "==MethodHandle";
	}

	public static void main(String[] args) throws Throwable {
		MethodHandleTest2 handle = new MethodHandleTest2();
		MethodType methodType = MethodType.methodType(String.class);
		
		MethodHandle mh = MethodHandles.lookup().findVirtual(MethodHandleTest2.class, "toString", methodType);
		String toString = (String) mh.invokeExact(handle);
		System.out.println(toString);
		
		// or like this:
//		MethodHandle mh2 = mh.bindTo(handle);
//		String toString2 = (String) mh2.invokeWithArguments();
//		System.out.println(toString2);
//		
		// 得到当前Class的不同表示方法，最后一个最好。一般我们在静态上下文用SLF4J得到logger用
//		System.out.println(MethodHandleTest2.class);
//		System.out.println(handle.getClass());
//		System.out.println(MethodHandles.lookup().lookupClass());
		
	}

}
